package com.lhc.project2;

public class CustomerView {
	//创建最大包含10客户对象的CustomerList对象
	CustomerList list = new CustomerList(10);
	
	//用途：创建CustomerView实例，并调用enterMainMenu()方法以执行程序
	public static void main(String[] args) {
		CustomerView cv = new CustomerView();
		cv.enterMainMenu();
	}
	
	//用途：显示主菜单，响应用户输入
	//根据用户操作分别调用其他相应的成员方法（如addNewCustomer),以完成客户信息处理
	public void enterMainMenu() {
		//定义开关（标记）
		boolean flag = true;
		while(flag) {
			System.out.println("---------------客户信息管理软件---------------");
			System.out.println();
			System.out.println("\t\t1.添加客户");
			System.out.println("\t\t2.修改客户");
			System.out.println("\t\t3.删除客户");
			System.out.println("\t\t4.客户列表");
			System.out.println("\t\t5.退    出");
			System.out.println();
			System.out.print("\t\t请选择（1-5）：");
			char key = CMUtility.readMenuSelection();
			switch(key) {
			//添加客户
			case '1':
				addNewCustomer();
				break;
			//修改客户
			case '2':
				modifyCustomer();
				break;
			//删除客户
			case '3':
				deleteCustomer();
				break;
			//客户列表
			case '4':
				listAllCustomer();
				break;
			//退出
			case '5':
				flag = exit();
				break;
			}
			
		}
	}

	private boolean exit() {
		return false;
	}

	private void listAllCustomer() {
		System.out.println("-----------------客户列表-----------------");
		System.out.println("编号\t姓名\t性别\t年龄\t手机\t邮箱");
		//获取所有客户对象的数组
		Customer[] cs = list.getAllCustomers();
		if(cs.length == 0 ) {
			System.out.println("系统中还没有客户信息，赶快添加吧......");
		}
		for (int i = 0; i < cs.length; i++) {
			Customer c = cs[i];
			
			System.out.println(c.getId() +"\t"+ c.getName()+"\t" + c.getGender() +"\t"+c.getAge()+"\t"+ c.getPhone()+"\t" + c.getEmail());
			
		}
		System.out.println("-----------------客户列表完成-----------------");
	}

	private void modifyCustomer() {
		System.out.println("-----------------修改客户-----------------");
		System.out.println("请选择待修改客户编号(-1退出):");
		int id = CMUtility.readInt();
		
		if(id == -1) {
			return;
		}
		
		//根据客户编号获取客户的索引
		int index = getIndex(id);
		
		//根据索引获取客户对象
		Customer c = list.getCustomer(index);
		if(c == null) {
			System.out.println("-----------------修改失败-----------------");
			return;
		}
		
		System.out.println("姓名"+"("+c.getName()+"):");
		String name = CMUtility.readString(4,c.getName());
		System.out.println("性别"+"("+c.getGender()+"):");
		char gender = CMUtility.readChar(c.getGender());
		System.out.println("年龄"+"("+c.getAge()+"):");
		int age = CMUtility.readInt(c.getAge());
		System.out.println("手机"+"("+c.getPhone()+"):");
		String phone = CMUtility.readString(11,c.getPhone());
		System.out.println("邮箱"+"("+c.getEmail()+"):");
		String email = CMUtility.readString(25,c.getEmail());
		
		Customer cust = new Customer(name,gender,age,phone,email);
		//将原来的客户编号存入新的客户中
		cust.setId(c.getId());
		//将客户标准类中num - 1
		Customer.setNum(Customer.getNum() - 1);
		
		boolean result = list.replaceCustomer(index, cust);
		if(result) {
			System.out.println("-----------------修改成功-----------------");
		}else {
			System.out.println("-----------------修改失败-----------------");
		}
	}

	private void deleteCustomer() {
		System.out.println("-----------------删除客户-----------------");
		System.out.println("请选择待删除客户编号(-1退出):");
		int id = CMUtility.readInt();
		if(id == -1) {
			return;
		}
		System.out.println("确认是否决定删除Y/N");
		//获取是否删除的命令
		char key = CMUtility.readConfirmSelection();
		if (key == 'Y' || key == 'y') {
			//根据客户编号获取索引
			int index = getIndex(id);
			//根据索引进行删除
			boolean result = list.deleteCustomer(index);
			
			if(result) {
				System.out.println("-----------------删除成功-----------------");
			}else {
				System.out.println("-----------------删除失败-----------------");
			}
		}
		
		
	}

	private int getIndex(int id) {
		//获取所有客户对象数组
		Customer[] cs = list.getAllCustomers();
		
		//遍历数组中所有的客户对象
		for(int i = 0; i < cs.length; i++) {
			//获取每个客户对象
			Customer c = cs[i];
			if(id == c.getId()) {
				return i;
			}
		}
		return -1;
	}

	private void addNewCustomer() {
		System.out.println("-----------------添加客户-----------------");
		System.out.println("姓名");
		String name = CMUtility.readString(4);
		System.out.println("性别");
		char gender = CMUtility.readChar();
		System.out.println("年龄");
		int age = CMUtility.readInt();
		System.out.println("手机");
		String phone = CMUtility.readString(11);
		System.out.println("邮箱");
		String email = CMUtility.readString(25);
		
		//通过上述五个参数，创建客户对象
		Customer customer = new Customer(name,gender,age,phone,email);
		//将创建好的客户对象添加到list添加客户的方法
		boolean result = list.addCustomer(customer);
		
		if(result) {
			System.out.println("-----------------添加成功-----------------");
		}else {
			System.out.println("-----------------添加失败-----------------");
		}
	}
}
